package com.miguo.db;

import android.content.Context;

import com.miguo.app.AppContext;
import com.miguo.bean.AdvertBean;
import com.miguo.db.AdvertDao.Properties;
import com.miguo.utils.DateUtils;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

public class AdvertDB {
    private static Context mContext;
    private static AdvertDB instance;

    private AdvertDao advertDao;

    private AdvertDB() {
    }

    public static AdvertDB getInstance(Context context) {
        if (instance == null) {
            instance = new AdvertDB();
            if (mContext == null) {
                mContext = context;
            }

            // 数据库对象
            DaoSession daoSession = AppContext.getDaoSession(mContext,
                    false);
            instance.advertDao = daoSession.getAdvertDao();
        }
        return instance;
    }

    public static AdvertDB getInstance(Context context, boolean ClearSession) {
        if (instance == null) {
            instance = new AdvertDB();
            if (mContext == null) {
                mContext = context;
            }

            // 数据库对象
            DaoSession daoSession = AppContext.getDaoSession(mContext,
                    ClearSession);
            instance.advertDao = daoSession.getAdvertDao();
        }
        return instance;
    }

    public List<AdvertBean> loadAll() {
        return advertDao.loadAll();
    }

    public List<AdvertBean> findPosition(int position) {
        QueryBuilder<AdvertBean> ad = advertDao.queryBuilder();
        Long Time = DateUtils.getTime();
        ad.where(Properties.Position.eq(position),
                Properties.Start_time.le(Time), Properties.End_time.ge(Time));
        ad.orderAsc(Properties.Edit_time);// 排序依据
        return ad.list();
    }

    public List<AdvertBean> query() {
        QueryBuilder<AdvertBean> ad = advertDao.queryBuilder();
        Long Time = DateUtils.getTime();
        ad.where(Properties.Start_time.le(Time), Properties.End_time.ge(Time));
        ad.orderAsc(Properties.Edit_time);// 排序依据
        return ad.list();
    }

    /**
     * insert or update note
     *
     * @param advert
     * @return insert or update note id
     */
    public long save(AdvertBean advert) {
        return advertDao.insertOrReplace(advert);
    }

    /**
     * 查询
     */
    public boolean isSave(int Id) {
        QueryBuilder<AdvertBean> qb = advertDao.queryBuilder();
        qb.where(Properties.Id.eq(Id));
        qb.buildCount().count();
        return qb.buildCount().count() > 0 ? true : false;
    }

    /**
     * insert or update noteList use transaction
     *
     * @param list
     */
    public void saveList(final List<AdvertBean> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        advertDao.getSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < list.size(); i++) {
                    AdvertBean note = list.get(i);
                    advertDao.insertOrReplace(note);
                }
            }
        });

    }

    public boolean isExist() {
        QueryBuilder<AdvertBean> qb = advertDao.queryBuilder();
        return qb.buildCount().count() > 0 ? true : false;
    }

    /**
     * delete all
     */
    public void deleteAll() {
        try {
            advertDao.deleteAll();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}